package edu.hut.bookshop.service.impl;

import edu.hut.bookshop.dao.CommentMapper;
import edu.hut.bookshop.dao.UserMapper;
import edu.hut.bookshop.pojo.Comment;
import edu.hut.bookshop.pojo.User;
import edu.hut.bookshop.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;
    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    @Override
    public int insertCom(Comment comment ) {
        Comment comments = new Comment();
        User user = userMapper.selectByUserId(comment.getUserId());
        comments.setUserId(comment.getUserId());
        comments.setComment(comment.getComment());
        comments.setJiajuId(comment.getJiajuId());
        comments.setUserName(user.getUserName());
        if(comment.getParentId()!=null){
            comments.setParentId(comment.getParentId());
        }else {
            comments.setParentId(Long.parseLong("0"));
        }
        comments.setCcreateTime(new Date());
        //System.out.println(comments);
        int rows = commentMapper.insertCom(comments);
        return rows;
    }

    @Override
    public Comment selectBycId(Long cId) {
        return commentMapper.selectBycId(cId);
    }

    @Override
    public List<Comment> selectByjiajuId(Integer jiajuId) {
        List<Comment> comments = commentMapper.selectByjiajuId(jiajuId);
        return comments;
    }

    @Override
    public List<Comment> selectcomByuserId(Integer userId) {
        List<Comment> comments = commentMapper.selectcomByuserId(userId);
        return comments;
    }

    @Override
    public int deletecomBycId(Long cId) {
       int rows =  commentMapper.deletecomByuserId(cId);
        return rows;
    }


    /*1根据id为“-1”查询出所有父评论
      2根据父评论的id查询出一级子回复
      3根据子回复的id循环迭代查询出所有子集回复
      4将查询出来的子回复放到一个集合中*/
    @Override
    public List<Comment> findcomment(Integer jiajuId) {
        //查询出父节点
        List<Comment> comments = commentMapper.findByparentIdcfj(Long.parseLong("-1"),jiajuId);
        for(Comment comment : comments){
            //先找到id
            Long id = comment.getcId();
            String parentNickname1 = comment.getUserName();
            List<Comment> childComments = new ArrayList<>();
            if(commentMapper.findParentbyIdcyj(id)!=null){
                childComments = commentMapper.findParentbyIdcyj(id);
            }

            //查询出子评论
            if(childComments!=null){
                combineChildren(childComments, parentNickname1);
                comment.setReplyComments(tempReplys);
                tempReplys = new ArrayList<>();
            }

        }
        return comments;

    }

    /**
     * 查询出子评论
     * @param childComments
     * @param parentNickname1
     */
    private void combineChildren(List<Comment> childComments, String parentNickname1) {
        //判断是否有一级回复
        if (childComments.size() > 0) {
            //循环找出子评论的id
            for (Comment childComment : childComments) {
                String parentNickname = childComment.getUserName();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getcId();
                //查询二级以及所有子集回复
                recursively(childId, parentNickname);
            }
        }
    }

    /**
     * 循环迭代找出子集回复
     * @param childId
     * @param parentNickname1
     */
    private void recursively(Long childId, String parentNickname1){
        //根据一级评论的id找到二级评论
        List<Comment> replayComments = new ArrayList<>();
              replayComments =  commentMapper.findByReplayId(childId);
        if (replayComments!=null&&replayComments.size()>0){
            for(Comment replayComment : replayComments){
                String parentNickname = replayComment.getUserName();
                replayComment.setUserName(parentNickname1);
                Long replayId = replayComment.getcId();
                tempReplys.add(replayComment);

                //循环迭代找出子集回复
                recursively(replayId,parentNickname);
            }
        }


    }

}
